#include "frmaddemployee.h"
#include "ui_frmaddemployee.h"
#include "Used_head.h"
#include "employeecontainer.h"

FrmAddEmployee::FrmAddEmployee(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::FrmAddEmployee)
{
    ui->setupUi(this);
}

FrmAddEmployee::~FrmAddEmployee()
{
    delete ui;
}

bool FrmAddEmployee::judge(int id, string name, string phone, string depart){
    if(id <=0 || name == "" || phone == "" || depart == ""){
        QMessageBox::critical(this, "错误", "添加失败！可能原因:\n编号小于1\n姓名、电话或部门为空！");
        return false;
    }
    for(int i = 0; i < EmployeeContainer::employeelist->size(); i++){
        Employee *emp = EmployeeContainer::employeelist->at(i);
        if(id == emp->id){
            QMessageBox::critical(this, "错误", "添加失败！存在编号相同的员工！");
            return false;
        }
    }
    return true;
}

void FrmAddEmployee::on_btnAddandContinue_clicked()
{
    //构建对象
    int id = ui->letId->text().toInt();
    string name = ui->letName->text().toStdString();
    string phone = ui->letPhone->text().toStdString();
    string depart = ui->letDepart->text().toStdString();
    Employee *emp = new Employee(id, name, phone, depart);
    //将员工添加到容器中
    if(judge(id, name, phone, depart)){
        EmployeeContainer::employeelist->push_back(emp);
        ui->letId->clear();
        ui->letName->clear();
        ui->letPhone->clear();
        ui->letDepart->clear();
    }
}

void FrmAddEmployee::on_btnAddandClose_clicked()
{
    //构建对象
    int id = ui->letId->text().toInt();
    string name = ui->letName->text().toStdString();
    string phone = ui->letPhone->text().toStdString();
    string depart = ui->letDepart->text().toStdString();
    Employee *emp = new Employee(id, name, phone, depart);
    //将员工添加到容器中
    if(judge(id, name, phone, depart)){
        EmployeeContainer::employeelist->push_back(emp);
        this->close();
    }
}
